using System;
using Raven.Client;
using Sponsorship.Domain;
using Sponsorship.Domain.Persons;

namespace Sponsorship.ReadModel.ListOfSponsoredChildren
{
    /// <summary>
    /// Handles the events required to implement a view that displays a list of <see cref="ListOfSponsoredPersonsItemDto"/> objects.
    /// </summary>
    public class ListOfSponsoredPersonsEventHandler :
        IEventHandler<PersonEntered>
    {
        private readonly IDocumentStore _documentStore;

        public ListOfSponsoredPersonsEventHandler (IDocumentStore documentStore)
        {
            _documentStore = documentStore;
        }

        public void Handle (PersonEntered domainEvent)
        {
            using (IDocumentSession session = _documentStore.OpenSession())
            {
                var dto = new ListOfSponsoredPersonsItemDto
                              {
                                  AggregateRootId = domainEvent.PersonId,
                                  Name = domainEvent.FirstName + " " + domainEvent.LastName
                              };

                session.Store (dto);

                SaveChanges (session, dto, domainEvent);
            }
        }

        //public void Handle(PersonAssociated domainEvent)
        //{
        //    using (IDocumentSession session = _documentStore.OpenSession())
        //    {
        //        ListOfSponsoredPersonsItemDto dto = LoadPersonById(session, domainEvent.PersonId);

        //        // TODO: How to get the first name and last name of the associated person?
        //        // dto.AssociatedPersons.Add (domainEvent.PersonFirstName + " " + domainEvent.PersonLastName);
        //        dto.AssociatedPersons.Add("Dummy");
        //        SaveChanges(session, dto, domainEvent);
        //    }
        //}



        //public void Handle (SponsoredChildAssociated domainEvent)
        //{
        //    using (IDocumentSession session = _documentStore.OpenSession())
        //    {
        //        ListOfSponsoredChildrenItemDto dto = LoadChildById (session, domainEvent.SponsoredChildId);

        //        // TODO: How to get the first name and last name of the associated child?
        //        // dto.AssociatedPersons.Add (domainEvent.OtherChildLastName + " " + domainEvent.OtherChildLastName);
        //        dto.AssociatedPersons.Add ("Dummy");
        //        SaveChanges (session, dto, domainEvent);
        //    }
        //}

        //public void Handle (SponsoredChildNameChanged domainEvent)
        //{
        //    using (IDocumentSession session = _documentStore.OpenSession())
        //    {
        //        ListOfSponsoredChildrenItemDto dto = LoadChildById (session, domainEvent.SponsoredChildId);

        //        dto.Name = domainEvent.FirstName + " " + domainEvent.LastName;

        //        SaveChanges (session, dto, domainEvent);
        //    }
        //}

        //public void Handle (SponsoredChildEntered domainEvent)
        //{
        //    using (IDocumentSession session = _documentStore.OpenSession())
        //    {
        //        var dto = new ListOfSponsoredChildrenItemDto
        //                      {
        //                          AggregateRootId = domainEvent.SponsoredChildId,
        //                          Name = domainEvent.FirstName + " " + domainEvent.LastName
        //                      };

        //        session.Store (dto);

        //        SaveChanges (session, dto, domainEvent);
        //    }
        //}

        private static ListOfSponsoredPersonsItemDto LoadPersonById (IDocumentSession session, Guid childId)
        {
            return session.Load<ListOfSponsoredPersonsItemDto> (Dto.GetDtoIdOf<ListOfSponsoredPersonsItemDto> (childId));
        }

        private static void SaveChanges (IDocumentSession session, ListOfSponsoredPersonsItemDto dto, IEvent domainEvent)
        {
            // Getting the event version out of the event headers
            dto.Version = domainEvent.GetVersion();

            session.SaveChanges();
        }
    }
}